//
//  ViewController.m
//  26-Homework
//
//  Created by dllo on 16/8/18.
//  Copyright © 2016年 ZHW. All rights reserved.
//

#import "ViewController.h"
#import "Masonry.h"

@interface ViewController ()
@property (nonatomic, strong) UILabel *changedTime;
@property (nonatomic, strong) UILabel *endTime;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    UIImageView *backgroundImageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    backgroundImageView.image = [UIImage imageNamed:@"vc_bg"];
    [self.view addSubview:backgroundImageView];
    [self creatSubViews];
}

- (void)creatSubViews {
    UIButton *miniButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:miniButton];
    [miniButton setImage:[UIImage imageNamed:@"miniplayer_btn_playlist_close_b"] forState:UIControlStateNormal];
    [miniButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.top.mas_equalTo(10);
        make.width.mas_equalTo(50);
        make.width.mas_equalTo(50);
    }];
    
    UIButton *allMusicButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [allMusicButton setImage:[UIImage imageNamed:@"allMusic_more"] forState:UIControlStateNormal];
    [self.view addSubview: allMusicButton];
    [allMusicButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-10);
        make.top.mas_equalTo(miniButton);
        make.width.mas_equalTo(miniButton);
        make.height.mas_equalTo(miniButton);
    }];
 
    UILabel *songNameLabel = [[UILabel alloc] init];
    songNameLabel.text = @"阿猫阿狗";
    songNameLabel.textColor = [UIColor whiteColor];
    songNameLabel.font = [UIFont systemFontOfSize:25];
    [self.view addSubview:songNameLabel];
    [songNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(25);
        make.centerX.mas_equalTo(0);
        
        make.height.mas_equalTo(20);
    }];
    
    UILabel *authorLabel = [[UILabel alloc] init];
    authorLabel.textColor = [UIColor whiteColor];
    authorLabel.text = @"陈奕迅";
    [self.view addSubview:authorLabel];
    [authorLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(songNameLabel.mas_bottom).offset(10);
        make.height.mas_equalTo(20);
    }];
    
    UIButton *freeWifiButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [freeWifiButton setBackgroundImage:[UIImage imageNamed:@"player_btn_chinaunicom_normal"] forState:UIControlStateNormal];
    [freeWifiButton setBackgroundImage:[UIImage imageNamed:@"player_btn_chinaunicom_highlight"] forState:UIControlStateHighlighted];
    [self.view addSubview:freeWifiButton];
    [freeWifiButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0).multipliedBy(0.5f);
        make.top.mas_equalTo(authorLabel.mas_bottom).offset(5);
        make.height.mas_equalTo(25);
    }];
    
    UIButton *sqlButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [sqlButton setBackgroundImage:[UIImage imageNamed:@"player_btn_sq_sel_normal"] forState:UIControlStateNormal];
    [sqlButton setBackgroundImage:[UIImage imageNamed:@"player_btn_sq_sel_highlight"] forState:UIControlStateHighlighted];
    [self.view addSubview:sqlButton];
    [sqlButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(freeWifiButton.mas_right).offset(5);
        make.top.mas_equalTo(authorLabel.mas_bottom).offset(5);
        make.height.mas_equalTo(freeWifiButton);
    }];
    
    UIButton *mvButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [mvButton setBackgroundImage:[UIImage imageNamed:@"player_btn_mv_normal"] forState:UIControlStateNormal];
    [mvButton setBackgroundImage:[UIImage imageNamed:@"player_btn_mv_highlight"] forState:UIControlStateHighlighted];
    [self.view addSubview:mvButton];
    [mvButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(sqlButton.mas_right).offset(5);
        make.top.mas_equalTo(authorLabel.mas_bottom).offset(5);
        make.height.mas_equalTo(sqlButton);
    }];
    
    UIButton *dtsButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [dtsButton setBackgroundImage:[UIImage imageNamed:@"player_btn_dts_off"] forState:UIControlStateNormal];
    [self.view addSubview:dtsButton];
    [dtsButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(mvButton.mas_right).offset(5);
        make.top.mas_equalTo(authorLabel.mas_bottom).offset(5);
        make.height.mas_equalTo(mvButton);
    }];

    UIImageView *photoImageView = [[UIImageView alloc] init];
    photoImageView.image = [UIImage imageNamed:@"the_key"];
    photoImageView.layer.cornerRadius = 125;
    photoImageView.layer.masksToBounds = YES;
    [self.view addSubview:photoImageView];
    [photoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.centerY.mas_equalTo(self.view).multipliedBy(0.85f);
        make.width.mas_equalTo(250);
        make.height.mas_equalTo(250);
    }];
    
    UISlider *slider = [[UISlider alloc] init];
    slider.minimumValue = 0.00f;
    slider.maximumValue = 3.49f;
    [slider setMinimumTrackImage:[UIImage imageNamed:@"player_slider_playback_left"] forState:UIControlStateNormal];
    [slider setMaximumTrackImage:[UIImage imageNamed:@"player_slider_playback_ritght"] forState:UIControlStateNormal];
    [slider setThumbImage:[UIImage imageNamed:@"player_slider_playback_thumb"] forState:UIControlStateNormal];
    [slider addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:slider];
    [slider mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(photoImageView.mas_bottom).offset(30);
        make.width.mas_equalTo(self.view).multipliedBy(0.6f);
        make.height.mas_equalTo(20);
    }];
    
    self.changedTime = [[UILabel alloc] init];
    _changedTime.textColor = [UIColor whiteColor];
    _changedTime.text = @"00:00";
    [self.view addSubview:_changedTime];
    [_changedTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(slider.mas_left).offset(-5);
        make.top.mas_equalTo(slider);
        make.height.mas_equalTo(20);
    }];
    
    self.endTime = [[UILabel alloc] init];
    _endTime.text = [NSString stringWithFormat:@"0%.2f", slider.maximumValue];
    _endTime.textColor = _changedTime.textColor;
    [self.view addSubview:_endTime];
    [_endTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(slider.mas_right).offset(5);
        make.top.mas_equalTo(_changedTime.mas_top);
    }];
    
    UIPageControl *pageControl = [[UIPageControl alloc] init];
    pageControl.numberOfPages = 3;
    [self.view addSubview:pageControl];
    [pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(photoImageView.mas_bottom).offset(5);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(20);
    }];
    
    // 播放相关button
    UIButton *playButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [playButton setBackgroundImage:[UIImage imageNamed:@"player_btn_play_normal"] forState:UIControlStateNormal];
    [playButton setBackgroundImage:[UIImage imageNamed:@"player_btn_play_highlight"] forState:UIControlStateHighlighted];
    [playButton addTarget:self action:@selector(playButtonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:playButton];
    [playButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);

        make.centerY.mas_equalTo(self.view).multipliedBy(1.6);
        make.width.mas_equalTo(self.view).multipliedBy(0.2f);
    }];
    
    UIButton *preButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [preButton setBackgroundImage:[UIImage imageNamed:@"player_btn_pre_normal"] forState:UIControlStateNormal];
    [preButton setBackgroundImage:[UIImage imageNamed:@"player_btn_pre_highlight"] forState:UIControlStateHighlighted];
    [self.view addSubview:preButton];
    [preButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(playButton.mas_left).offset(-10);
        make.centerY.mas_equalTo(playButton.mas_centerY);
        make.width.mas_equalTo(self.view).multipliedBy(0.18f);
    }];
    
    UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [nextButton setBackgroundImage:[UIImage imageNamed:@"player_btn_next_normal"] forState:UIControlStateNormal];
    [nextButton setBackgroundImage:[UIImage imageNamed:@"player_btn_next_highlight"] forState:UIControlStateHighlighted];
    [self.view addSubview:nextButton];
    [nextButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(playButton.mas_right).offset(10);
        make.centerY.mas_equalTo(preButton.mas_centerY);
        make.width.mas_equalTo(preButton.mas_width);
    }];
    
    // 底部五个button
    NSArray *normalImages = @[@"player_btn_favorited_normal", @"player_btn_repeat_highlight", @"player_btn_downloaded_normal_1", @"player_btn_share_normal", @"player_btn_playlist_normal"];
    NSArray *highlightImages = @[@"player_btn_favorited_highlight", @"player_btn_repeat_highlight", @"player_btn_downloaded_highlight", @"player_btn_share_highlight", @"player_btn_playlist_highlight"];
    UIButton *lastButton = nil;
    int number = 5;
    CGFloat margin = 10;
    for (int i = 0; i < number; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setBackgroundImage:[UIImage imageNamed:normalImages[i]] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:highlightImages[i]] forState:UIControlStateHighlighted];
        button.tag = 1001 + i;
        [self.view addSubview:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            if (i % number == 0) {
                make.left.mas_equalTo(0);
            } else {
                make.left.mas_equalTo(lastButton.mas_right).offset(margin);
            }
            if (i % 5 == 4) {
                make.right.mas_equalTo(0);
            }
            make.bottom.mas_equalTo(-margin);
            make.width.mas_equalTo((self.view.frame.size.width - (number - 1) * margin) / number);
            make.height.mas_equalTo(60);
        }];
        lastButton = button;
    }
    
}

- (void)sliderValueChanged:(UISlider *)slider {
    _changedTime.text = [NSString stringWithFormat:@"0%.2f", slider.value];
}

- (void)playButtonDidClicked:(UIButton *)button {
    [button removeTarget:self action:@selector(playButtonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    [button setBackgroundImage:[UIImage imageNamed:@"player_btn_pause_normal"] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"player_btn_pause_highlight"] forState:UIControlStateHighlighted];
    [button addTarget:self action:@selector(pauseButtonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)pauseButtonDidClicked:(UIButton *)button {
    [button removeTarget:self action:@selector(pauseButtonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    [button setBackgroundImage:[UIImage imageNamed:@"player_btn_play_normal"] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"player_btn_play_normal_highlight"] forState:UIControlStateHighlighted];
    [button addTarget:self action:@selector(playButtonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
